const fs = require('fs');
const oc=require("./octree")

// const data = fs.readFileSync('./tmp/data.txt');
const data = fs.readFileSync('./tmp/test.pcl');
function toArrayBuffer(str) {
    let ab = new ArrayBuffer(str.length);
    countTotal=str.length;
    let view = new Uint8Array(ab);
    for (let i = 0; i < str.length; ++i) {
        view[i] = str[i];
    }
    // console.log(ab);
    // console.log(view);
    return ab;
}
let countTotal=0;
let count = 0;
let bf=toArrayBuffer(data);
const view = new DataView(bf);
let arr=[]
// console.log(countTotal);
function DesiralizeDfs(depth, offset) {
    if (depth == 0) {
        count += 1;
        arr.push(offset);
    } else {
        const idx = view.getUint8(offset, false);       
        offset += 1;
        for (let i = 0; i < 8; i++) {
            if (idx & (1 << i)) {
                offset = DesiralizeDfs(depth - 1, offset);
            }
        }
    }
    return offset;
}
var flag_view=0;
function Desiralize() {
    let offset = 0;
    console.log(view.getBigUint64(offset, false));
    offset += 8; // xyz length
    for (let i = 0; i < 6; i++) {
        console.log(view.getFloat32(offset + i * 4, false));
    }
    offset += 6 * 4; // min xyz max xyz
    const depth = Number(view.getBigInt64(offset, false));
    console.log(depth);
    offset += 8; // depth
    if(offset>countTotal){
        console.log("超出");
    }else{
        offset=DesiralizeDfs(depth, offset);
    }
    console.log("leaf count: ", count, " depth: ", depth, " offset : ", offset);
    console.log(view.getBigUint64(offset, false));
}

// Desiralize();
let bf2=toArrayBufferView(data);
// console.log("arr",arr);

oc.decodeOcTree("",bf2);

function toArrayBufferView(str) {
    let ab = new ArrayBuffer(str.length);
    countTotal=str.length;
    let view = new Uint8Array(ab);
    for (let i = 0; i < str.length; ++i) {
        view[i] = str[i];
    }
    // console.log(ab);
    // console.log(view);
    return view;
}

